package com.pms.utils;

import org.w3c.dom.*;
import org.xml.sax.InputSource;
import javax.xml.transform.*;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;


public class XmlUtility {

	/**
	 * @param args
	 */
	public static String getL1NodeValueFrmNodeList(NodeList nls){
		Element tempElement = (Element)nls.item(0);
		NodeList tempNodeList = tempElement.getChildNodes();
		return ((Node) tempNodeList.item(0)).getNodeValue();
	}
	
    public static String convertDomToString(Document dom){
		

	    TransformerFactory transfac = TransformerFactory.newInstance();
	    Transformer trans = null;
	    try{
	    	trans = transfac.newTransformer();
	    }catch(TransformerConfigurationException e){
	    	e.printStackTrace();
	    }
	    trans.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "yes");
	    trans.setOutputProperty(OutputKeys.INDENT, "yes");

	   
	    StringWriter sw = new StringWriter();
	    StreamResult result = new StreamResult(sw);
	    DOMSource source = new DOMSource(dom);
	    try{
	    	trans.transform(source, result);
	    }catch(TransformerException e){
	    	e.printStackTrace();
	    }
	    
	    return sw.toString();
	    
	    
	}
    
    public static Document convertStringToDom(String inputXML){
		Document doc=null;
				
		try{
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			doc = db.parse(new InputSource(new StringReader(inputXML)));
			
			
		}catch (Exception e) {
			e.printStackTrace();
		} 
		
		return doc;
	 
	}

}
